package com.example.pingze.controller;

import com.example.pingze.entity.BannerPicture;
import com.example.pingze.service.BannerService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;

/**
 * 轮播图
 */
@RestController
@RequestMapping("/banner")
@CrossOrigin(origins = "*")
public class BannerController {

    @Autowired
    private BannerService service;
    List<String> baseList = new ArrayList();

    //获取轮播图
    @PostMapping("/getBanner")
    public Result<List> getBanner() throws IOException {

        Calendar calendar = Calendar.getInstance();
        int date = calendar.get(Calendar.DATE);

        int pageNum = date / 7 + 1;
        int pageSize = 4;
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page page = service.getBannerInfo(pageable);
        List<BannerPicture> list = (List<BannerPicture>) page.getContent();
        for (BannerPicture bannerPicture : list) {
            String address = bannerPicture.getBannerAddress();
            String baseAddress = toBase(address);
            baseList.add(baseAddress);
        }
        return ResultVOUtils.result(baseList);
    }

    public String toBase(String address) throws IOException {
        Path path = Paths.get(address);
        byte[] addressBytes = Files.readAllBytes(path);
        String addressData = Base64.getMimeEncoder().encodeToString(addressBytes);
        return addressData;
    }


//    private List<String> bannerList = null;
//    private BannerPicture banner = null;

    /**
     * 将存储在数据库的4张轮播图，使用分页将图片以流的形式传给前端
     * @param response
     * @throws IOException
     */
//    @PostMapping("/getBanner")
//    public void getBanner(HttpServletResponse response) throws IOException {
//        Date date = new Date();
//        int pageNum = date.getDate();
//        List<String> list = getBannerList(pageNum);
//        response.setContentType("image/jpeg");
//        OutputStream out = response.getOutputStream();
//        for(String bannerList : list){
//            InputStream in = new URL(bannerList).openStream(); //获取该路径的图片输入流
//            byte[] bytes = new byte[1024];
//            int bytesRead = 0;
//            while ((bytesRead = in.read(bytes)) != -1){
//                out.write(bytes, 0, bytesRead);
//            }
//            in.close();
//        }
//        out.close();
//    }
    /**
     * 将分页的轮播图路径存放到list中
     * @param pageNum
     * @throws IOException
     */
//    public List<String> getBannerList(Integer pageNum){
//        bannerList = new ArrayList<>();
//        int pageSize = 4;
//        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
//        Page page = service.getBannerInfo(pageable);
//        List<BannerPicture> list = (List<BannerPicture>) page.getContent();
//        for(BannerPicture banner : list){
//            String address = banner.getBannerAddress();
//            bannerList.add(address);
//        }
//        return bannerList;
//    }

    /**
     * 上传轮播图资源，将图片保存在本地，并将图片路径存放在数据库中
     * @param file
     * @throws IOException
     */
//    @PostMapping("/uploadImg")
//    public void uploadBanner(@RequestParam("image") MultipartFile file) throws IOException {
//        if(file != null){
//            String fileName = file.getOriginalFilename();
//            String[] fileNameArray = fileName.split("\\.");
//            String uuid = UUID.randomUUID().toString();
//            String filePath = "D:/banner/" + uuid + "." + fileNameArray[1];
//            banner = new BannerPicture();
//            banner.setBannerAddress(filePath);
//            service.saveBanner(banner);
//            FileCopyUtils.copy(file.getBytes(),new File(filePath));
//        }
//    }
}
